package org.study.minio.boot.controller;

import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.study.minio.boot.config.AwsTemplate;
import org.study.minio.boot.config.MinioConstant;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * aws对外提供服务的接口
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2023年10月13日
 */
@AllArgsConstructor
@RestController
@RequestMapping("/aws")
public class AwsController {
    private final AwsTemplate awsTemplate;

    @PostMapping("/bucket/{bucketName}")
    public Bucket createBucket(@PathVariable String bucketName) {
        awsTemplate.createBucket(bucketName);
        Bucket bucket = awsTemplate.getBucket(bucketName).get();
        return bucket;
    }

    @GetMapping("/bucket")
    public List<Bucket> getBuckets() {
        List<Bucket> bucketList = awsTemplate.getAllBuckets();
        return bucketList;
    }

    @GetMapping("/bucket/{bucketName}")
    public Bucket getBucket(@PathVariable String bucketName) {
        Bucket bucket = awsTemplate.getBucket(bucketName)
                .orElseThrow(() -> new RuntimeException("Bucket不存在"));
        return bucket;
    }

    @DeleteMapping("/bucket/{bucketName}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void deleteBucket(@PathVariable String bucketName) {
        awsTemplate.removeBucket(bucketName);
    }

    @PostMapping("/object")
    public String putObject(@RequestParam("file") MultipartFile file) throws IOException {
        // 多文件上传参数：@RequestParam("files") MultipartFile[] files
        String name = file.getOriginalFilename();
        String url = awsTemplate.putObject(MinioConstant.BUCKET_TEST, name, file.getInputStream(), file.getSize(), file.getContentType());
        return url;
    }

    @PostMapping("/object/{objectName}")
    public String putObject(@PathVariable String objectName, @RequestParam("file") MultipartFile file) throws IOException {
        // 多文件上传参数：@RequestParam("files") MultipartFile[] files
        String url = awsTemplate.putObject(MinioConstant.BUCKET_TEST, objectName, file.getInputStream(), file.getSize(), file.getContentType());
        return url;
    }

    @GetMapping("/object/{objectName}")
    public List<S3ObjectSummary> filterObject(@PathVariable String objectName) {
        List<S3ObjectSummary> objectSummaryList = awsTemplate.getAllObjectsByPrefix(MinioConstant.BUCKET_TEST, objectName, true);
        return objectSummaryList;
    }

    @GetMapping("/object/{objectName}/{expires}")
    public Map<String, Object> getObject(@PathVariable String objectName, @PathVariable Integer expires) {
        Map<String, Object> responseBody = Maps.newHashMap();
        responseBody.put("bucket", MinioConstant.BUCKET_TEST);
        responseBody.put("object", objectName);
        responseBody.put("url", awsTemplate.getObjectURL(MinioConstant.BUCKET_TEST, objectName, expires));
        responseBody.put("expires", expires);
        return responseBody;
    }

    @DeleteMapping("/object/{objectName}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void deleteObject(@PathVariable String objectName) {
        awsTemplate.removeObject(MinioConstant.BUCKET_TEST, objectName);
    }
}
